﻿using Ortodoctor.gridComponent;
using Ortodoctor.modelo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ortodoctor.Funcionario
{
    public partial class Parcelas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USUARIO"] == null)
                Response.Redirect("Login.aspx");

            modeloParcelas model = new modeloParcelas();
            Grid grid = new Grid(model.getAll(), "id_parcela");
            Session["GRID_PARCELAS"] = grid;

            cliente.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            procedimento.ClientIDMode = System.Web.UI.ClientIDMode.Static;

            modeloCliente modelo = new modeloCliente();
            DataTable dt = modelo.getAll();
            dt.DefaultView.Sort = "[nome] ASC";
            dt = dt.DefaultView.ToTable();

            cliente.Items.Add(new ListItem(" - Todos os clientes", "99999999"));
            foreach (DataRow dr in dt.Rows)
            {
                cliente.Items.Add(new ListItem(dr["nome"].ToString(), dr["id_cliente"].ToString()));
            }
            
            modeloProcedimento modeloproc = new modeloProcedimento();
            dt = modeloproc.getAll();
            dt.DefaultView.Sort = "[nome_procedimento] ASC";
            dt = dt.DefaultView.ToTable();

            procedimento.Items.Add(new ListItem(" - Todos os procedimentos", "99999999"));
            foreach (DataRow dr in dt.Rows)
            {
                procedimento.Items.Add(new ListItem(dr["nome_procedimento"].ToString(), dr["id_procedimento"].ToString()));
            }
        }

        [WebMethod]
        public static string getDados(string page, string rp, string sortname, string sortorder, string query, string qtype)
        {
            Grid grid = ((Grid)HttpContext.Current.Session["GRID_PARCELAS"]);
            grid.page = Convert.ToInt32(page);
            DataTable dt = new DataTable();
            foreach (DataColumn dc in grid.dados.Columns)
                dt.Columns.Add(dc.ColumnName, typeof(string));

            if (string.IsNullOrEmpty(query))
            {
                int numeroItensPagina = Convert.ToInt32(rp);
                int numeroPagina = Convert.ToInt32(page);
                int inicio = numeroItensPagina * (numeroPagina - 1);

                for (int i = inicio; i < inicio + numeroItensPagina && i < grid.dados.Rows.Count; ++i)
                {
                    DataRow dr = dt.NewRow();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.ColumnName.Equals("data_vencimento"))
                            dr[dc.ColumnName] = Convert.ToDateTime(grid.dados.Rows[i][dc.ColumnName]).ToString("dd/MM/yyyy");
                        else
                            dr[dc.ColumnName] = grid.dados.Rows[i][dc.ColumnName];
                    }

                    dt.Rows.Add(dr);
                }
                if (dt.Rows.Count > 0)
                {
                    dt.DefaultView.Sort = sortname + " " + sortorder;
                    dt = dt.DefaultView.ToTable();
                }
            }
            else
            {
                dt = grid.dados.Copy();
                dt.DefaultView.RowFilter = string.Format("[{0}] like '%{1}%'", qtype, query);
                dt = dt.DefaultView.ToTable();

                DataTable dtemp = new DataTable();
                foreach (DataColumn dc in dt.Columns)
                    dtemp.Columns.Add(dc.ColumnName, typeof(System.String));

                foreach (DataRow dr in dt.Rows)
                {
                    DataRow drow = dtemp.NewRow();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.ColumnName.Equals("data_vencimento"))
                            drow["data_vencimento"] = Convert.ToDateTime(dr["data_vencimento"]).ToString("dd/MM/yyyy");
                        else
                            drow[dc.ColumnName] = dr[dc.ColumnName];
                    }
                    dtemp.Rows.Add(drow);
                }
                dt = dtemp;
            }
            return grid.getDadosToJson(dt);
        }

        [WebMethod]
        public static string editTable(int id_parcela, string newValue)
        {
            //System.Windows.Forms.MessageBox.Show("oi, stol aki");
            modeloParcelas parcela = new modeloParcelas();
            parcela.updateStatus(new Componentes.Parcelas(id_parcela, 0.0, DateTime.Now, 0, newValue));
            Grid grid = ((Grid)HttpContext.Current.Session["GRID_PARCELAS"]);
            grid.dados.Select("[id_parcela]=" + id_parcela)[0]["status"] = newValue;
            HttpContext.Current.Session["GRID_PARCELAS"] = grid;
            return "editou";
        }

        [WebMethod]
        public static string refreshData(int idCliente, int idProcedimento)
        {
            modeloParcelas model = new modeloParcelas();
            Grid grid = new Grid(model.getParcelas(idCliente, idProcedimento), "id_parcela");
            HttpContext.Current.Session["GRID_PARCELAS"] = grid;
            return "ok";
        }

        [WebMethod]
        public static string getPermissao()
        {
            return HttpContext.Current.Session["USUARIO_ACESSO"].ToString();
        }

        [WebMethod]
        public static string getStatus(string id_parcela)
        {

            modeloParcelas p = new modeloParcelas();
            
            string s = p.get(id_parcela);
            //System.Windows.Forms.MessageBox.Show(s);
            return s;
        }

        [WebMethod]
        public static void edit(int id, float novoValor)
        {
            modeloParcelas p = new modeloParcelas();
            p.updateValor(id, novoValor);
        }
    }
}